package com.party.web.web.controller.circle;

import com.party.common.constant.Constant;
import com.party.common.paging.Page;
import com.party.core.model.circle.Circle;
import com.party.core.model.circle.CircleTopicTag;
import com.party.core.service.circle.ICircleService;
import com.party.core.service.circle.ICircleTopicTagService;
import com.party.core.service.circle.biz.CircleTopicBizService;
import com.party.core.utils.MyBeanUtils;
import com.party.web.web.annotation.ControlParam;
import com.party.web.web.annotation.DataControl;
import com.party.web.web.dto.AjaxResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 圈子话题类型
 */
@Controller
@RequestMapping(value = "/circle/topicTag")
public class CircleTopicTagController {

	@Autowired
	private CircleTopicBizService circleTopicBizService;

	@Autowired
	private ICircleTopicTagService circleTopicTagService;
	@Autowired
	private ICircleService circleService;
	/**
	 * 圈子话题标签列表页面
	 */
	@DataControl
	@RequestMapping(value = {"list", ""})
	public ModelAndView list(Page page, @ControlParam(table = "circle", column = "create_by", getMethodName = "getCircle") CircleTopicTag tag) {
		page.setLimit(20);
		ModelAndView mv = new ModelAndView("circle/topicTagList");
		List<CircleTopicTag> list = circleTopicTagService.listPage(tag, page);
		Circle circle = circleService.get(tag.getCircle());
		mv.addObject("circle", circle);
		mv.addObject("tag", tag);
		mv.addObject("list", list);
		mv.addObject("page", page);
		return mv;
	}

	/**
	 * 跳转至发布
	 *
	 * @param circleId
	 * @param id
	 * @return
	 */
	@DataControl
	@RequestMapping(value = "form")
	public ModelAndView form(@ControlParam(table = "circle", column = "create_by") String circleId, String id) {
		ModelAndView mv = new ModelAndView("circle/topicTagForm");
		if (StringUtils.isNotEmpty(id)) {
			CircleTopicTag circleTag = circleTopicTagService.get(id);
			if (null == circleTag) {
				return new ModelAndView(Constant.ERROR_402);
			}
			mv.addObject("tag", circleTag);
		}
		Circle circle = circleService.get(circleId);
		mv.addObject("circle", circle);
		mv.addObject("circleId", circleId);
		return mv;
	}

	/**
	 * 删除类型
	 */
	@ResponseBody
	@RequestMapping(value = "del")
	public AjaxResult del(CircleTopicTag circleTopicTag) throws Exception {
		AjaxResult ajaxResult = new AjaxResult();
		try {
			circleTopicBizService.delTagBiz(circleTopicTag);
		} catch (Exception e) {
			ajaxResult.setSuccess(false);
		}
		ajaxResult.setSuccess(true);
		return ajaxResult;
	}
	/**
	 * 添加类型
	 *
	 */
	@ResponseBody
	@RequestMapping(value = "save")
	public AjaxResult reject(CircleTopicTag tag) throws Exception {
		AjaxResult ajaxResult = new AjaxResult();
		try {
			if (StringUtils.isNotEmpty(tag.getId())) {// 编辑表单保存
				CircleTopicTag dbTag = circleTopicTagService.get(tag.getId());
				MyBeanUtils.copyBeanNotNull2Bean(tag, dbTag);// 将编辑表单中的非\NULL值覆盖数据库记录中的值
				circleTopicTagService.update(dbTag);
			}else {
				String tagId = circleTopicTagService.insert(tag);
				tag = circleTopicTagService.get(tagId);
				ajaxResult.setData(tag);
			}
		} catch (Exception e) {
			ajaxResult.setSuccess(false);
		}
		ajaxResult.setSuccess(true);
		return ajaxResult;
	}



}
